/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.upeu.examlp1.comm;

import java.sql.*;
/**
 *
 * @author TOSHIBA
 */
public class Conexion {
    Connection conn = null;
    public Connection getConnection(){
        try {
            String sc = "jdbc:mysql://localhost:3306/upeu?user=root&password=";
            Class.forName("com.mysql.jdbc.Driver");
            this.conn = DriverManager.getConnection(sc);
            System.out.println("Conectado");
        } catch (Exception e) {
            System.out.print("Error: " +e.getMessage());
        }        
        return this.conn;
    }
    public void close(){
        try {
            if( this.conn!=null)
                this.conn.close();
        } catch (Exception e) {
            System.out.print("Error: " +e.getMessage());
        }        
    }    

    public static void main ( String[] arg ) {
        
        Connection conn = null;
        try {
            String sc = "jdbc:mysql://localhost:3306/upeu?user=root&password=";
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(sc);
            System.out.println("Conectado");
            
            String qry="insert into User(Name, Email, Cellular) values(?,?,?)";
            PreparedStatement ps=conn.prepareStatement(qry);
            ps.setString(1, "Angel Sullon");
            ps.setInt(2, 4);
            int r=ps.executeUpdate();
            System.out.println(r+" registros add in User table");
            
            qry="select * from User";
            ps=conn.prepareStatement(qry);
            ResultSet rs= ps.executeQuery();
            while(rs.next())
                System.out.println(rs.getString("UserId")+ " "+rs.getString("Name"));

        } catch (Exception e) {
            System.out.print("Error: " +e.getMessage());
        }
        //return conn;
    }

}

